<?php

require_once(dirname(__FILE__).'/DB/Note.php');

define('STATUS_UNDONE'        , 0);
define('STATUS_DONE'          , 1);

define('STATUS_PRIVATE'       , 0);
define('STATUS_PUBLIC'        , 1);
define('STATUS_LIMITED_PUBLIC', 2);

class Note {
    public    $id         = null;
    public    $member_id  = null;
    public    $title      = '';
    public    $date       = 0;
    public    $time       = null;
    protected $progress   = 0;
    protected $status     = 0;

    public function __construct ($id_or_raw_info = null) {
        if (is_array($id_or_raw_info)) {
            $todo = $id_or_raw_info;

            if (preg_match('/^0+-0+-0+$/', $todo['date'])) {
                $todo['date'] = 0;
            }

            $this->id        = $todo['id'];
            $this->member_id = $todo['member_id'];
            $this->title     = $todo['title'];
            $this->date      = $todo['date'];
            $this->time      = $todo['time'];
            $this->progress  = $todo['progress'];
            $this->status    = $todo['status'];
        }
        else if (isset($id_or_raw_info) && is_numeric($id_or_raw_info)) {
            $id = $id_or_raw_info;
            $this->load($id);
        }
    }

    public function save () {
        if (isset($this->id)) {
            return $this->update();
        }

        is_numeric($this->member_id) or die;

        $db = new DB_Note('mst');
        $id = $db->insert(
            $this->member_id,
            $this->title,
            $this->date,
            $this->time,
            $this->progress,
            $this->status
        );
        $this->id = $id;
        return $id;
    }

    public function load ($id) {
        is_numeric($id) or die;

        $db = new DB_Note('slv');
        $todo = $db->select_by_id($id);

        if (preg_match('/^0+-0+-0+$/', $todo['date'])) {
            $todo['date'] = 0;
        }

        $this->id        = $id;
        $this->member_id = $todo['member_id'];
        $this->title     = $todo['title'];
        $this->date      = $todo['date'];
        $this->time      = $todo['time'];
        $this->progress  = $todo['progress'];
        $this->status    = $todo['status'];
    }

    public function update () {
        is_numeric($this->id) or die;

        $db = new DB_Note('mst');
        $result = $db->update(
            $this->id,
            $this->title,
            $this->date,
            $this->time,
            $this->progress,
            $this->status
        );
        return $result;
    }

    public function delete () {
        is_numeric($this->id) or die;

        $db = new DB_Note('mst');
        $result = $db->delete(
            $this->id
        );
        if ($result) {
            $this->id        = null;
            $this->member_id = null;
            $this->title     = null;
            $this->date      = null;
            $this->time      = null;
            $this->progress  = null;
            $this->status    = null;
        }

        return $result;
    }

    public function set_done () {
        is_numeric($this->id) or die;

        $this->progress = STATUS_DONE;
        return $this->update();
    }

    public function set_undone () {
        is_numeric($this->id) or die;
        $this->progress = STATUS_UNDONE;
        return $this->update();
    }

    public function set_private () {
        is_numeric($this->id) or die;
        $this->status = STATUS_PRIVATE;
        return $this->update();
    }

    public function set_public () {
        is_numeric($this->id) or die;
        $this->status = STATUS_PUBLIC;
        return $this->update();
    }

    public function set_date ($date) {
        is_numeric($this->id) or die;
        $this->date = $date;
        return $this->update();
    }

    public function is_done () {
        is_numeric($this->id) or die;
        return isset($this->progress) && $this->progress == STATUS_DONE;
    }

    public function is_public () {
        is_numeric($this->id) or die;
        return isset($this->status) && $this->status == STATUS_PUBLIC;
    }

    public function unix_stamp () {
        if (!$this->date) {
            return null;
        }
        $date = preg_match('/^\d+-\d+-\d+$/', $this->date)
            ? $this->date : '0-0-0';
        $time = preg_match('/^\d+:\d+:\d+$/', $this->time)
            ? $this->time : '0:0:0';
        list($y, $m, $d) = explode('-', $date);
        list($h, $i, $s) = explode(':', $time);
        return mktime($h, $i, $s, $m, $d, $y);
    }
}
